package com.zzh.lib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.zzh.lib.views.def.IExpandMoreCallback;

import androidx.annotation.RequiresApi;

/**
 * Created by ZZH on 2019-12-17.
 *
 * @Date: 2019-12-17
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 自动适配ScrollView的嵌套
 */
public class AutoListView extends ListView {

    private View footMoreView;
    private boolean isEnableExpandMore, isAddFootMoreView = false, isManualRemove = false;

    public AutoListView(Context context) {
        super(context);
    }


    public AutoListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    private void initAttrs(Context ctx, AttributeSet attrs) {
        TypedArray mTypedArray = ctx.obtainStyledAttributes(attrs, R.styleable.AutoListView);
        if (mTypedArray.hasValue(R.styleable.AutoListView_zh_foot_more_view)) {
            int layoutResId = mTypedArray.getResourceId(R.styleable.AutoListView_zh_foot_more_view, 0);
            if (layoutResId != 0) {
                footMoreView = LayoutInflater.from(ctx).inflate(layoutResId, this, false);
                if (footMoreView != null) {
                    footMoreView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("-----", "--------");
                        }
                    });
                }
            }
        }
        mTypedArray.recycle();
    }

    public AutoListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    public View getFootMoreView() {
        return footMoreView;
    }

    public void setFootMoreView(View footMoreView) {
        this.footMoreView = footMoreView;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public AutoListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttrs(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
        if (adapter instanceof IExpandMoreCallback && footMoreView != null) {
            adapter.registerDataSetObserver(new DataSetObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    int count = ((IExpandMoreCallback) adapter).getRealCount();
                    int maxItem = ((IExpandMoreCallback) adapter).getMaxItem();
                    isEnableExpandMore = maxItem > 0 && count > maxItem && footMoreView != null;
                    if (isEnableExpandMore && !isAddFootMoreView && !isManualRemove) {
                        isAddFootMoreView = true;
                        addFooterView(footMoreView);
                    }

                    if (maxItem > count && footMoreView != null && isAddFootMoreView) {
                        isAddFootMoreView = false;
                        removeFooterView(footMoreView);
                    }

                }

                @Override
                public void onInvalidated() {
                    super.onInvalidated();
                }
            });

        }
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

    }

    /**
     * 重置手动删除
     *
     * @return
     */
    public AutoListView resetManualRemove() {
        isManualRemove = false;
        return this;
    }

    /**
     * 手动删除扩展布局
     */
    public void removeExpandMoreView() {
        if (footMoreView != null && isAddFootMoreView) {
            isAddFootMoreView = false;
            isManualRemove = true;
            removeFooterView(footMoreView);
        }
    }
}
